package com.novel.frame.lgw.Reptile;

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Demo {
	
	//获取小说
	public static List<String> getNovel(int pn,int page) {
		 List<String>  Novel = new ArrayList<String>();
		for (int i = pn; i <=page;) {
		Connection conn = Jsoup.connect("http://www.xbiquge.la/fenlei/"+i+"_1.html");
//		Connection conn = Jsoup.connect("http://www.xbiquge.la/");
		try {
			Document doc = conn.get();
			Elements eles = doc.select("div[id=newscontent]").select("div[class=l]").
					select("li").select("span[class=s2]").select("a");
//			System.out.println(eles);			
	     	for (Element element : eles) {
				String title = element.text();
				String href = element.attr("href");
//				System.out.println("标题："+title+",链接："+href);
					Novel.add(title+","+href);
			}
			 i++;
		} catch (IOException e) {
			System.err.println("获取小说错误");
//			e.printStackTrace();
		}		
	  }
		return Novel;	
 }	
	//获取章节
	public static List<String> getChapter(String url) {
		//1.得到这个访问的页面
		//http://www.xbiquge.la/13/13959/5939025.html
		//http://www.xbiquge.la/13/13959/5939026.html
//		http://www.xbiquge.la/14/14930/
		 List<String>  Chapter = new ArrayList<String>();
		Connection conn = Jsoup.connect(url);
		try {
			//文档
			Document doc = conn.get();
			//System.out.println(doc);
			//2.截取页面的内容
			Elements eles = doc.select("div[id=list]").select("dl").select("dd").select("a");
			//System.out.println(eles);
			for (Element element : eles) {
				String title = element.text();
				String href = element.attr("href");
//				System.out.println("标题："+title+",链接："+href);
				if (element.text().contains("章")) {
					Chapter.add(title+","+href);
				}		
			}
			//http://www.xbiquge.la+href
			return Chapter;
		} catch (IOException e) {
//			e.printStackTrace();
			System.err.println("获取章节失败");
		}
		return null;
	}
	//获取内容
	public static String getContent(String url) {
		//1.得到这个访问的页面
		//http://www.xbiquge.la/13/13959/5939025.html
		//http://www.xbiquge.la/13/13959/5939026.html
//		/14/14930/21582222.html
		Connection conn = Jsoup.connect("http://www.xbiquge.la"+url);
		String content="";
		try {
			//文档
			Document doc = conn.get();
			//System.out.println(doc);
			//2.截取页面的内容
			Elements eles = doc.select("div[id=content]");
			//System.out.println(eles.html());
			//3.去掉br（回车）和&nbsp;（空格）
		    content= eles.html();
			content = content.replaceAll("<br>", "").replaceAll("&nbsp;", " ");
			content =content.substring(0, content.lastIndexOf("<p>"));
			return content;
		} catch (IOException e) {
//			e.printStackTrace();
			System.err.println("获取内容失败");
		}
		return "本章读取错误";
	}
	//下载小说
	public static int downloadNovel(File path,String content) {		 
		Writer	w=null;
		BufferedWriter bw=null;
		if ("本章读取错误".equals(content)) {
		   return 0;	
		}
		try {
			w = new FileWriter(path);
			bw=new  BufferedWriter(w);
			bw.write(content);
			bw.newLine();
			bw.flush();
			System.out.println("下载成功========》下载路劲是"+path);
			return 1;
		} catch (IOException e) {
			System.err.println("下载失败");
		} finally {
			try {
				if (w != null) {
					w.close();
				}
				if (bw != null) {
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	return 0;	
	}
	
//	http://www.xbiquge.la/fenlei/1_1.html
	public static void main(String[] args) {		
//    List<String> Novel=Demo.getNovel(1,1);
    List<String> Novel=Demo.getNovel(3,3);
    for (int i = 0; i <Novel.size();) {	     	
    String Noveltitle=Novel.get(i).substring(0,Novel.get(i).lastIndexOf(","));
    String Novelhref=Novel.get(i).substring(Novel.get(i).indexOf(",")+1);
    //小说        
    List<String> Chapter=Demo.getChapter(Novelhref);
    if (Chapter!=null) {
        if (Chapter.size()<500) {
            File file=new File("D://小说项目/书库/"+Noveltitle);	
            if(!file.exists()) {
        		 file.mkdirs();
        	System.out.println(Noveltitle+"创建路径=====》"+file);
            }else {
            System.out.println(Noveltitle+"存在路径=====》"+file); 
                i++;
                continue;
            }
          for (int j = 0; j <Chapter.size();) {
      		String title=Chapter.get(j).substring(0,Chapter.get(j).lastIndexOf(","));
      		String href=Chapter.get(j).substring(Chapter.get(j).indexOf(",")+1);
        	  File fileChapter=new File("D://小说项目/书库/"+Noveltitle+"/"+title+".txt");
        	  if (!fileChapter.exists()) {
				try {
					fileChapter.createNewFile();		
				if (downloadNovel(fileChapter, getContent(href))==1) {
					 j++;
//			   System.out.println(title+"下载路径=====》"+fileChapter);
				}else {
					System.err.println("==============================================下载失败"+title);
				  }	
				} catch (IOException e) {
				  System.err.println(title+"创建失败=====》"+fileChapter); 
					if (title.contains("/")||title.contains("\\")||title.contains("*")
							||title.contains(":")||title.contains("?")||title.contains("\"")||
							title.contains("<")||title.contains(">")||title.contains("|")) {			 				
						
						title=title.replace("/", "").replace("\\", "").replace("*", "")
						    .replace(":", "").replace("?", "").replace("\"", "").
						    replace("<", "").replace(">", "").replace("|", "").replace("？", "");
					}
				}
			}else {
			System.out.println(title+"存在路径=====》"+fileChapter); 
			if (downloadNovel(fileChapter, getContent(href))==1) {
				 j++;
//		   System.out.println(title+"下载路径=====》"+fileChapter);
			}else {
				System.err.println("==============================================下载失败"+title);
			  }	
			}
		  }
    	}
        i++;
	}else {
		System.out.println("章节内容为空");
	}

	

//		List<String> Chapter=Demo.getChapter("http://www.xbiquge.la/14/14930/");
//		for (int i1 = 0; i1 <Chapter.size();) {
//			String title=Chapter.get(i1).substring(0,Chapter.get(i1).lastIndexOf(","));
//			String href=Chapter.get(i1).substring(Chapter.get(i1).indexOf(",")+1);
//			System.out.println(title+"===="+href);
//			File file=new File("D://demo/书库小说/"+title+".txt");
//			if (!file.exists()) {
//				try {
//					file.createNewFile();
//			   System.out.println("创建路径"+file);
//				if (downloadNovel(file, getContent(href))==1) {
//					i1++;
//				}else {
//					System.out.println("下载失败"+title);
//				  }	
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}else {
//				i1++;
//			}		
//		}
		
//		http://www.xbiquge.la/15/15409/8163818.html
//		/14/14930/21582222.html
//		downloadNovel(file, getContent(value));	
  
//图片，名字，类型，总字数，简介，更新时间，状态
       }
    }
}